Skip to content

fix(github-webhook): /ok-to-test is not triggering CI on PRs#2682

Open
zakisk wants to merge 2 commits intotektoncd:mainfrom
zakisk:fix-ok-to-test-on-gh-webhook
Open

fix(github-webhook): /ok-to-test is not triggering CI on PRs#2682
zakisk wants to merge 2 commits intotektoncd:mainfrom
zakisk:fix-ok-to-test-on-gh-webhook

Conversation

@zakisk
Copy link
Copy Markdown
Member

@zakisk zakisk commented Apr 14, 2026

📝 Description of the Change

When an unauthorized user opens a pull request on a repository
configured with Pipelines-as-Code using GitHub webhook
integration, commenting /ok-to-test as an admin does not trigger
the CI pipeline. This happens because the GitHub client (ghClient)
is never initialized for webhook-based issue comment events — the
client setup only ran for GitHub App events during payload parsing.

Root cause:

  • In the issue_comment handler, the code checked if ghClient was
    nil and returned an error, but for webhook integrations the
    client is legitimately nil at that point since webhooks
    authenticate differently from GitHub Apps.
  • The PR number was being extracted by parsing the HTML URL string
    instead of reading it directly from the event object.
  • The webhook request payload and headers were not being preserved
    on the event object, which is needed for webhook signature
    validation.

Changes:

  • pkg/provider/github/parse_payload.go:

    • Add initGitHubWebhookClient() to initialize the provider
      client for webhook-based events using
      gitclient.SetupAuthenticatedClient
    • Preserve request headers and payload on the event object
      early in ParsePayload so they are available for webhook
      signature validation
    • Reorder handleIssueCommentEvent to match the repository
      first, then lazily initialize the GitHub client if nil
      (webhook case), before fetching the pull request details
    • Use event.GetIssue().GetNumber() directly instead of parsing
      the PR number from the HTML URL string
    • Remove the early ghClient nil check that blocked webhook
      events
  • pkg/provider/github/github.go:

    • Move GitHub App token scoping logic from gitclient into
      SetClient, keeping provider-specific concerns within the
      provider package
  • pkg/gitclient/client_setup.go:

    • Remove GitHub App token scoping (moved to provider)
    • Add global repository lookup when globalRepo is nil, so
      webhook-based flows can resolve credentials from the global
      repository configuration
    • Replace github provider import with metav1 for the Get call
  • pkg/provider/github/parse_payload_test.go:

    • Remove test cases that asserted ghClient nil was an error
      (no longer applicable)
    • Remove test for invalid PR URL parsing (PR number now read
      from event)
    • Add Number field to IssueCommentEvent test fixtures
  • pkg/provider/github/acl_test.go:

    • Add html_url and number to issue comment test payload to
      match new handleIssueCommentEvent flow that sets URL and
      PR number from the event object
  • pkg/provider/github/github_test.go:

    • Add Logger, pacInfo, and repo with Settings to SetClient
      test to support token scoping moved into SetClient
  • pkg/gitclient/client_setup_test.go:

    • Add GlobalRepository and Namespace to test seed data to
      match new global repo lookup
  • pkg/pipelineascode/pipelineascode_test.go:

    • Add GlobalRepository and Kube namespace to Run.Info to
      match new global repo lookup in SetupAuthenticatedClient
  • pkg/reconciler/reconciler_test.go:

    • Add Logger to Provider in reconciler test to support
      token scoping logging in SetClient

JIRA

https://redhat.atlassian.net/browse/SRVKP-11557

🔗 Linked GitHub Issue

Fixes #

🧪 Testing Strategy

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Manual testing
  • Not Applicable

🤖 AI Assistance

AI assistance can be used for various tasks, such as code generation,
documentation, or testing.

Please indicate whether you have used AI assistance
for this PR and provide details if applicable.

  • I have not used any AI assistance for this PR.
  • I have used AI assistance for this PR.

Important

Slop will be simply rejected, if you are using AI assistance you need to make sure you
understand the code generated and that it meets the project's standards. you
need at least know how to run the code and deploy it (if needed). See
startpaac to make it easy
to deploy and test your code changes.

If the majority of the code in this PR was generated by an AI, please add a Co-authored-by trailer to your commit message.
For example:

Co-authored-by: Claude noreply@anthropic.com

✅ Submitter Checklist

  • 📝 My commit messages are clear, informative, and follow the project's How to write a git commit message guide. The Gitlint linter ensures in CI it's properly validated
  • ✨ I have ensured my commit message prefix (e.g., fix:, feat:) matches the "Type of Change" I selected above.
  • ♽ I have run make test and make lint locally to check for and fix any
    issues. For an efficient workflow, I have considered installing
    pre-commit and running pre-commit install to
    automate these checks.
  • 📖 I have added or updated documentation for any user-facing changes.
  • 🧪 I have added sufficient unit tests for my code changes.
  • 🎁 I have added end-to-end tests where feasible. See README for more details.
  • 🔎 I have addressed any CI test flakiness or provided a clear reason to bypass it.
  • If adding a provider feature, I have filled in the following and updated the provider documentation:
    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 14, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 74.24242% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.96%. Comparing base (bd9f468) to head (76bb854).

Files with missing lines Patch % Lines
pkg/provider/github/github.go 33.33% 4 Missing and 2 partials ⚠️
pkg/provider/github/parse_payload.go 79.31% 4 Missing and 2 partials ⚠️
pkg/reconciler/reconciler.go 50.00% 2 Missing ⚠️
pkg/cmd/tknpac/webhook/add.go 0.00% 1 Missing ⚠️
pkg/cmd/tknpac/webhook/update-token.go 0.00% 1 Missing ⚠️
pkg/provider/gitlab/parse_payload.go 0.00% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2682      +/-   ##
==========================================
- Coverage   58.99%   58.96%   -0.04%     
==========================================
  Files         207      207              
  Lines       20363    20378      +15     
==========================================
+ Hits        12014    12015       +1     
- Misses       7578     7591      +13     
- Partials      771      772       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the codebase by moving authentication and secret management into dedicated gitclient and secrets packages, and relocates GitHub App token scoping logic to the GitHub provider. It also improves issue comment event handling by ensuring the client is initialized correctly. Feedback highlights a potential data race and unnecessary error logging when fetching the global repository, as well as a bug where trimming the webhook payload could break HMAC signature validation.

Comment thread pkg/gitclient/client_setup.go Outdated
Comment thread pkg/provider/github/parse_payload.go
@chmouel
Copy link
Copy Markdown
Member

chmouel commented Apr 14, 2026

reviewing this today, can you create and link a jira to it please? we want to track it in changelog downstream

@zakisk

@zakisk
Copy link
Copy Markdown
Member Author

zakisk commented Apr 14, 2026

reviewing this today, can you create and link a jira to it please? we want to track it in changelog downstream

@zakisk

yeah added I thought that I added it before

Comment thread pkg/provider/github/parse_payload_test.go
Comment thread pkg/provider/github/parse_payload.go Outdated
Comment thread pkg/gitclient/client_setup.go Outdated
@zakisk zakisk force-pushed the fix-ok-to-test-on-gh-webhook branch 5 times, most recently from 884b7ab to d7c9dd6 Compare April 16, 2026 04:14
zakisk added 2 commits April 17, 2026 16:48
this moves the git provider client setup logic to
its own package gitclient so that we can call it
from github package. at the moment if we call client
setup from github it creates import cycle.

Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
When an unauthorized user opens a pull request on a repository
configured with Pipelines-as-Code using GitHub webhook
integration, commenting /ok-to-test as an admin does not trigger
the CI pipeline. This happens because the GitHub client (ghClient)
is never initialized for webhook-based issue comment events — the
client setup only ran for GitHub App events during payload parsing.

Root cause:
- In the issue_comment handler, the code checked if ghClient was
  nil and returned an error, but for webhook integrations the
  client is legitimately nil at that point since webhooks
  authenticate differently from GitHub Apps.
- The PR number was being extracted by parsing the HTML URL string
  instead of reading it directly from the event object.
- The webhook request payload and headers were not being preserved
  on the event object, which is needed for webhook signature
  validation.

Changes:
- pkg/provider/github/parse_payload.go:
  - Add initGitHubWebhookClient() to initialize the provider
    client for webhook-based events using
    gitclient.SetupAuthenticatedClient
  - Preserve request headers and payload on the event object
    early in ParsePayload so they are available for webhook
    signature validation
  - Reorder handleIssueCommentEvent to match the repository
    first, then lazily initialize the GitHub client if nil
    (webhook case), before fetching the pull request details
  - Use event.GetIssue().GetNumber() directly instead of parsing
    the PR number from the HTML URL string
  - Remove the early ghClient nil check that blocked webhook
    events

- pkg/provider/github/github.go:
  - Move GitHub App token scoping logic from gitclient into
    SetClient, keeping provider-specific concerns within the
    provider package

- pkg/gitclient/client_setup.go:
  - Remove GitHub App token scoping (moved to provider)
  - Add global repository lookup when globalRepo is nil, so
    webhook-based flows can resolve credentials from the global
    repository configuration
  - Replace github provider import with metav1 for the Get call

- pkg/provider/github/parse_payload_test.go:
  - Remove test cases that asserted ghClient nil was an error
    (no longer applicable)
  - Remove test for invalid PR URL parsing (PR number now read
    from event)
  - Add Number field to IssueCommentEvent test fixtures

- pkg/provider/github/acl_test.go:
  - Add html_url and number to issue comment test payload to
    match new handleIssueCommentEvent flow that sets URL and
    PR number from the event object

- pkg/provider/github/github_test.go:
  - Add Logger, pacInfo, and repo with Settings to SetClient
    test to support token scoping moved into SetClient

- pkg/gitclient/client_setup_test.go:
  - Add GlobalRepository and Namespace to test seed data to
    match new global repo lookup

- pkg/pipelineascode/pipelineascode_test.go:
  - Add GlobalRepository and Kube namespace to Run.Info to
    match new global repo lookup in SetupAuthenticatedClient

- pkg/reconciler/reconciler_test.go:
  - Add Logger to Provider in reconciler test to support
    token scoping logging in SetClient

Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
@zakisk zakisk force-pushed the fix-ok-to-test-on-gh-webhook branch from d7c9dd6 to 76bb854 Compare April 17, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants